﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ELearning.Wpf.UI
{
    using ELearning.Data;
    using ELearning.Data.LearningDatasetTableAdapters;

    /// <summary>
    /// Interaction logic for wndSubjectSelect.xaml
    /// </summary>
    public partial class wndSubjectSelect : Window
    {
        public wndSubjectSelect()
        {
            InitializeComponent();

            RefreshData();
        }

        private void RefreshData()
        {
            using (SubjectsTableAdapter _tableAdapter = new SubjectsTableAdapter())
            {
                var table = _tableAdapter.GetData();
                List<Subject> _subjects = new List<Subject>(table.Count);
                foreach (LearningDataset.SubjectsRow row in table)
                {
                    _subjects.Add(new Subject { Name = row.Name, ID = row.ID });
                }

                lbSubjects.DataContext = _subjects;
            }
        }

        private void btAdd_Click(object sender, RoutedEventArgs e)
        {
            wndNameEnter name = new wndNameEnter();
            if (name.ShowDialog() == true)
            {
                using (SubjectsTableAdapter tableAdapter = new SubjectsTableAdapter())
                {
                    tableAdapter.Insert(name.NewName);
                }

                RefreshData();
            }
        }

        private void bt_ChangePassword_Click(object sender, RoutedEventArgs e)
        {
            wndNewPassword newPassword = new wndNewPassword(SessionManager.CurrentUserID, true);
            newPassword.ShowDialog();
        }

        private void lbSubjects_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (lbSubjects.SelectedItem == null) 
                return;

            wndSubjectView wndSubjectView = new wndSubjectView((lbSubjects.SelectedItem as Subject).ID, (lbSubjects.SelectedItem as Subject).Name);
            wndSubjectView.ShowDialog();
        }
    }

    public class Subject
    {
        public long ID { get; set; }
        public string Name { get; set; }
        public int PercentCompleted { get; set; }
    }
}
